package com.exonline.back.controller.order;

import com.exonline.commons.base.IBaseServlet;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @desc 订单管理c层
 * @author hbrj
 */
@WebServlet("/admin/order")
public class OrderServlet extends HttpServlet implements IBaseServlet {
    ObjectMapper mapper = new ObjectMapper();
    OrderService service = new OrderService();
    int pageno = 1;
    int pagesize = 4;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pagenoStr = req.getParameter("pageno");
        if (pagenoStr != null) {
            pageno = Integer.parseInt(pagenoStr);
        }
         String method = req.getParameter("method");
                 try {
                     if ("query".equals(method)) {
                         this.query(req, resp);
                     }else if("queryall".equals(method)){
                         this.queryAll(req, resp);
                     } else if ("queryone".equals(method)) {
                         this.queryOne(req, resp);
                     } else if ("add".equals(method)) {
                         this.add(req, resp);
                     } else if ("edit".equals(method)) {
                         this.edit(req, resp);
                     } else if ("delete".equals(method)) {
                         this.delete(req, resp);
                     }
                 } catch (Exception e) {
                     throw new ServletException(e);
                 }
    }
    /**
     * @desc 查询全部订单
     * @author hbrj
     */
    public void queryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String status;
        if (req.getHeader("Authorization") == null) {
             Map<String, Object> map = new HashMap<>();
                     map.put("code", 201);
                     map.put("msg", "查询失败,请确认您的登录状态");
                     map.put("data", map);
                     mapper.writeValue(resp.getWriter(), map);

        } else if (req.getParameter("status") != null) {
            status = req.getParameter("status");
            List list = service.queryAll(status,pagesize, pageno);
            String rowCount = service.getRowCount("SELECT COUNT(*) from orders");
            Map<String, Object> map = new HashMap<>();
            map.put("code", 200);
            map.put("msg", "查询成功");
            map.put("data",list);
            map.put("rowCount",rowCount);
            mapper.writeValue(resp.getWriter(), map);
        } else {
            List list = service.queryAll(pagesize, pageno);
            Map<String, Object> map = new HashMap<>();
            String rowCount = service.getRowCount("SELECT COUNT(*) from orders");
            map.put("code", 200);
            map.put("msg", "查询成功");
            map.put("data", list);
            map.put("rowCount",rowCount);
            mapper.writeValue(resp.getWriter(), map);
        }
    }
    /**
     * @desc 删除订单
     * @author hbrj
     */
    @Override
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        IBaseServlet.super.delete(req, resp);
    }
    /**
     * @desc 按订单名称进行查询
     * @author hbrj
     */
    @Override
    public void queryOne(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        try{
            String name;
           if(req.getParameter("name") != null) {
                name = req.getParameter("name");
                List<Map<String, Object>> stringMap = (List<Map<String, Object>>) service.queryOne(name);
                Map<String, Object> map = new HashMap<>();
                map.put("code", 200);
                map.put("msg", "查询成功");
                map.put("data", stringMap);
                mapper.writeValue(resp.getWriter(), map);
            }
        } catch (SQLException | IOException e) {
            throw new RuntimeException("查询订单时出现错误!"+e.getMessage(),e);
        }
    }

    @Override
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        IBaseServlet.super.edit(req, resp);
    }
}
